<template>
  <div class="app-container">
    <div class="receipt-content">
      <h1 class="receipt-title">收款凭据</h1>
      
      <div class="receipt-header">
        <div class="header-left">
          <span>账户：{{ receiptData.accountId }} 现金</span>
          <span style="margin-left: 20px">币别：人民币</span>
        </div>
        <div class="header-right">
          <span>{{ formatDate(receiptData.createAt) }}</span>
          <span style="margin-left: 20px">NO.{{ receiptData.journalNumber }}</span>
        </div>
      </div>

      <div class="receipt-table">
        <table>
          <tr>
            <td class="label">摘要</td>
            <td class="value">{{ receiptData.abstractMsg }}</td>
          </tr>
          <tr>
            <td class="label">大写金额</td>
            <td class="value">{{ convertToChinese(receiptData.debitAmount) }}</td>
          </tr>
          <tr>
            <td class="label">小写金额</td>
            <td class="value">{{ formatAmount(receiptData.debitAmount) }}</td>
          </tr>
          <tr>
            <td class="label">往来单位</td>
            <td class="value">{{ receiptData.contactUnitsId }}</td>
          </tr>
          <tr>
            <td class="label">收支类别</td>
            <td class="value">{{ receiptData.revenueId }}</td>
          </tr>
          <tr>
            <td class="label">结算方式</td>
            <td class="value">{{ receiptData.paymentMethodId }}</td>
          </tr>
          <tr>
            <td class="label">票据号</td>
            <td class="value">{{ receiptData.billNumber }}</td>
          </tr>
          <tr>
            <td class="label">备注</td>
            <td class="value">{{ receiptData.notes }}</td>
          </tr>
        </table>
      </div>

      <div class="receipt-footer">
        <div>会计：{{ receiptData.creator }}</div>
        <div>出纳：会计{{ receiptData.creator }}</div>
        <div>经手人：</div>
      </div>
    </div>

    <div class="receipt-actions">
      <el-button @click="goBack">返 回</el-button>
      <el-button type="primary" @click="handlePrint">打 印</el-button>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { parseTime } from '@/utils/ruoyi'
import { getJournal } from "@/api/journal/journal"
import { useRoute, useRouter } from 'vue-router'

const route = useRoute()
const router = useRouter()
const receiptData = ref({})

// 格式化日期
const formatDate = (date) => {
  return parseTime(date, '{y}年{m}月{d}日')
}

// 格式化金额
const formatAmount = (amount) => {
  if (!amount) return '0.00'
  return parseFloat(amount).toFixed(2)
}

// 金额转中文大写
const convertToChinese = (num) => {
  if(!num) return '零元整'
  const units = ['', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿']
  const cnNums = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
  
  let numStr = Number(num).toFixed(2)
  let integer = Math.floor(numStr)
  let decimal = Math.round((numStr - integer) * 100)
  
  let chineseStr = ''
  let integerStr = integer.toString()
  
  for(let i = 0; i < integerStr.length; i++) {
    let digit = parseInt(integerStr[i])
    chineseStr += cnNums[digit] + units[integerStr.length - 1 - i]
  }
  
  chineseStr += '元'
  
  if(decimal > 0) {
    let jiao = Math.floor(decimal / 10)
    let fen = decimal % 10
    if(jiao > 0) chineseStr += cnNums[jiao] + '角'
    if(fen > 0) chineseStr += cnNums[fen] + '分'
  } else {
    chineseStr += '整'
  }
  
  return chineseStr
}

// 返回上一页
const goBack = () => {
  router.go(-1)
}

// 打印功能
const handlePrint = () => {
  window.print()
}

onMounted(() => {
  const id = route.params.id
  if (id) {
    getJournal(id).then(response => {
      receiptData.value = response.data
    })
  }
})
</script>

<style scoped>
.app-container {
  background: #fff;
  padding: 20px;
}

.receipt-content {
  width: 800px;
  margin: 0 auto;
  min-height: 500px;
  padding: 40px;
  border: 1px solid #ddd;
}

.receipt-title {
  text-align: center;
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 30px;
}

.receipt-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
  padding: 10px 0;
  border-bottom: 1px solid #ddd;
}

.receipt-table {
  margin: 20px 0;
}

table {
  width: 100%;
  border-collapse: collapse;
}

td {
  padding: 12px;
  border: 1px solid #ddd;
}

.label {
  width: 120px;
  background-color: #f8f8f8;
  text-align: right;
  font-weight: bold;
}

.value {
  text-align: left;
  padding-left: 20px;
}

.receipt-footer {
  display: flex;
  justify-content: space-between;
  margin-top: 50px;
  padding-top: 20px;
  border-top: 1px solid #ddd;
}

.receipt-actions {
  text-align: center;
  margin-top: 20px;
}

@media print {
  .receipt-actions {
    display: none;
  }
  
  .receipt-content {
    border: none;
    padding: 0;
  }
}
</style> 